package com.product.android.utils;

import org.json.JSONException;
import org.json.JSONObject;

import com.common.android.utils.http.HttpException;
/**
 * 抛出HttpException辅助类
 * <br>Created 2014-7-30 下午2:35:46
 * @version  1.0
 * @author   huangszh		
 * @see 	 
 * Copyright(c) 2009-2014, TQ Digital Entertainment, All Rights Reserved
 *
 */
public class HttpErrMsgUtils {
	
    /**
     * 根据code和msg抛出HttpException
     * <br>Created 2014-6-20 下午6:45:20
     * @param retCode 返回码
     * @param object 错误内容{"msg":""}
     * @throws HttpException 请求异常
     * @author       huangszh
     */
    public static void parseHttpErrMsg(int retCode, JSONObject object)
            throws HttpException {

        parseHttpErrMsg(retCode, object.optString("msg"));
    }
    
    /**
     * 将形如{code:“001“,msg:”xxxx”}的JSON对象解析抛出HttpException
     * <br>Created 2014-7-30 下午2:32:05
     * @param object  {code:“001“,msg:”xxxx”}
     * @throws HttpException  请求异常
     * @author       huangszh
     */
    public static void parseHttpErrMsg(JSONObject object) throws HttpException {

        parseHttpErrMsg(object.optInt("code"), object.optString("msg"));
    }

    /**
     * 根据code和msg抛出HttpException
     * <br>Created 2014-6-20 下午6:44:5
     * @param retCode 返回码
     * @param msg 错误内容
     * @throws HttpException 请求异常
     * @author       huangszh
     */
    public static void parseHttpErrMsg(int retCode, String msg) throws HttpException {

        throw new HttpException(msg, retCode);
    }

    /**
     * 根据code和msg抛出HttpException
     * <br>Created 2014-6-20 下午6:44:42
     * @param retCode 返回码
     * @param receive 错误内容{"msg":""}
     * @throws HttpException 请求异常
     * @throws JSONException JSON异常
     * @author       huangszh
     */
    public static void parseHttpErrMsg(int retCode, StringBuilder receive)
            throws HttpException, JSONException {
        parseHttpErrMsg(retCode, new JSONObject(receive.toString()));

    }

    /**
     * 根据code和msg抛出HttpException
     * <br>Created 2014-8-22 下午1:46:33
     * @param retCode 返回码
     * @param receive 错误内容{"msg":""}
     * @throws HttpException 请求异常
     * @throws JSONException JSON异常
     * @author       huangszh
     */
    public static void parseHttpErrMsg(int retCode, StringBuffer receive)
            throws HttpException, JSONException {

        parseHttpErrMsg(retCode, new JSONObject(receive.toString()));

    }
}
